/*
 * @(#)BikeTripData.java   09/12/30
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package Example.Components;

import Example.Interfaces.BikeQS;
import Example.Interfaces.CourseQS;

import java.util.Date;
import java.util.Vector;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/12/30
 * @author         Huaxi (Yulin) ZHANG    
 */
public class BikeTripData implements BikeQS, CourseQS {

    /** Field description */
    public Vector bikes = new Vector();

    /** Field description */
    public Vector courses = new Vector();

    /**
     * Constructs ...
     *
     */
    public BikeTripData() {
        Object[] b1 = { Integer.valueOf(001), Double.valueOf(100.5), "Gare", Boolean.valueOf(true) };
        Object[] b2 = { Integer.valueOf(002), Double.valueOf(10.6), "Triolet", Boolean.valueOf(false) };
        Object[] b3 = { Integer.valueOf(003), Double.valueOf(11.5), "Gare", Boolean.valueOf(false) };
        Object[] b4 = { Integer.valueOf(004), Double.valueOf(30.6), "Triolet", Boolean.valueOf(false) };
        Object[] b5 = { Integer.valueOf(005), Double.valueOf(150.5), "Gare", Boolean.valueOf(false) };
        Object[] b6 = { Integer.valueOf(006), Double.valueOf(1.6), "Triolet", Boolean.valueOf(false) };

        bikes.add(b1);
        bikes.add(b2);
        bikes.add(b3);
        bikes.add(b4);
        bikes.add(b5);
        bikes.add(b6);

        Object[] u1 = { Integer.valueOf(001), Double.valueOf(10), Boolean.valueOf(true), Integer.valueOf(001),
                        "1232550255063" };
        Object[] u2 = { Integer.valueOf(002), Double.valueOf(20), Boolean.valueOf(false), Integer.valueOf(0), "" };
        Object[] u3 = { Integer.valueOf(003), Double.valueOf(30), Boolean.valueOf(false), Integer.valueOf(0), "" };

        courses.add(u1);
        courses.add(u2);
        courses.add(u3);
    }

    /**
     * Method description
     *
     * @param bikeID  
     * @param location  
     * @param trip  
     * @return  
     */
    public boolean saveBike(int bikeID, String location, double trip) {
        Object[] b = null,
                 a = null;
        int      j = 0;

        for (int i = 0; i < bikes.size(); i++) {
            b = (Object[]) bikes.get(i);

            if (trip == 0) {
                if (((Integer) b[0]).intValue() == bikeID && b[3].toString().equals("false")) {
                    j    = i;
                    b[3] = Boolean.valueOf(true);
                    a    = b;
                }
            } else {
                if (((Integer) b[0]).intValue() == bikeID && b[3].toString().equals("true")) {
                    j    = i;
                    b[3] = Boolean.valueOf(false);

                    double d = Double.parseDouble(b[1].toString()) + trip;

                    b[1] = Double.valueOf(d);
                    b[2] = location;
                    a    = b;
                }
            }
        }

        bikes.add(j, a);

        return false;
    }

    /**
     * Method description
     *
     * @param userID  
     * @param bikeID  
     * @return  
     */
    public String saveCourse(int userID, int bikeID) {
        Object[] a    = null,
                 b    = null;
        String   time = null;
        int      j    = 88888888;

        for (int i = 0; i < courses.size(); i++) {
            b = (Object[]) courses.get(i);

            if (((Integer) b[0]).intValue() == userID && b[2].toString().equals("false")) {
                b[2] = Boolean.valueOf(true);
                b[3] = Integer.valueOf(bikeID);

                long l = System.currentTimeMillis();
                Date s = new Date(l);

                b[4] = s.toString();
                j    = i;
                a    = b;
            } else if (((Integer) b[0]).intValue() == userID && b[2].toString().equals("true")) {
                b[2] = Boolean.valueOf(false);
                b[3] = Integer.valueOf(0);
                time = (String) b[4];
                b[4] = "";
                j    = i;
                a    = b;
            }
        }

        if (j != 88888888) {
            courses.add(j, a);

            Object[] c = (Object[]) courses.get(1);

            return time;
        }

        return "";
    }

    /**
     * Method description
     *
     * @param locationStart  
     * @return  
     */
    public Vector findBike(String locationStart) {
        Vector bikesF = new Vector();

        if (locationStart.equals("Gare") | locationStart.equals("Triolet")) {
            for (int i = 0; i < bikes.size(); i++) {
                Object[] b = (Object[]) bikes.get(i);

                if (b[2].equals(locationStart) && b[3].equals(Boolean.valueOf(false))) {
                    bikesF.add(b);
                }
            }
        } else {
            for (int i = 0; i < bikes.size(); i++) {
                Object[] b = (Object[]) bikes.get(i);

                if (b[3].equals(Boolean.valueOf(false))) {
                    bikesF.add(b);
                }
            }
        }

        return bikesF;
    }

    /**
     * Method description
     *
     * @param userID  
     * @return  
     */
    public Object[] findCourse(int userID) {
        Object[] c = null;

        for (int i = 0; i < courses.size(); i++) {
            c = (Object[]) courses.get(i);

            if (((Integer) c[0]).intValue() == userID) {
                System.out.println(c[4]);

                return c;
            }
        }

        return null;
    }
}
